<?php

namespace App\Jobs;



use App\Libraries\Tools\ConstVar;
use App\Libraries\Tools\Msg;
use App\Libraries\Tools\Tools;
use App\Models\MLM\MLMBalance;
use App\Models\MLM\MLMBalanceLog;
use App\Models\User\Balance;
use App\Models\MLM\MLM;
use App\Models\User\User;
use Illuminate\Support\Facades\DB;

class MLMJob extends Job
{


    private $user_id;

    public function __construct($user_id)
    {
        $this->user_id = $user_id;
    }

    public function handle(MLM $MLM, User $user, MLMBalanceLog $MLMBalanceLog)
    {
        $mlm = $MLM->where('user_id', $this->user_id)->first();
        $user = $user->where('user_id', $this->user_id)->first();
        if(empty($mlm) || empty($user)){
            throw new \Exception('fail on user_id='.$this->user_id);
        }
        $discount = \App\Libraries\Tools\MLM::getVIPDiscount($this->user_id);
        DB::beginTransaction();
        try{
            if($mlm->s_parent_id){
                $MLMBalanceLog->add(
                    $this->user_id,
                    $mlm->s_parent_id,
                    ConstVar::$VIP_COMMISSION_S_PARENT * $discount,
                    ConstVar::$MLM_BALANCE_LOG_TYPE_USER
                );
                $mlm_balance = MLMBalance::firstOrCreate(['user_id' => $mlm->s_parent_id]);
                $mlm_balance->increment('balance', ConstVar::$VIP_COMMISSION_S_PARENT * $discount);
                $mlm_balance->increment('total_balance', ConstVar::$VIP_COMMISSION_S_PARENT * $discount);
            }
            if($mlm->m_parent_id){
                $MLMBalanceLog->add(
                    $this->user_id,
                    $mlm->m_parent_id,
                    ConstVar::$VIP_COMMISSION_M_PARENT * $discount,
                    ConstVar::$MLM_BALANCE_LOG_TYPE_USER
                );
                $mlm_balance = MLMBalance::firstOrCreate(['user_id' => $mlm->m_parent_id]);
                $mlm_balance->increment('balance', ConstVar::$VIP_COMMISSION_M_PARENT * $discount);
                $mlm_balance->increment('total_balance', ConstVar::$VIP_COMMISSION_M_PARENT * $discount);
            }
            if($mlm->l_parent_id){
                $MLMBalanceLog->add(
                    $this->user_id,
                    $mlm->l_parent_id,
                    ConstVar::$VIP_COMMISSION_L_PARENT * $discount,
                    ConstVar::$MLM_BALANCE_LOG_TYPE_USER
                );
                $mlm_balance = MLMBalance::firstOrCreate(['user_id' => $mlm->l_parent_id]);
                $mlm_balance->increment('balance', ConstVar::$VIP_COMMISSION_L_PARENT * $discount);
                $mlm_balance->increment('total_balance', ConstVar::$VIP_COMMISSION_L_PARENT * $discount);
            }

            //代理核算
            if($mlm->l_agent_id && $this->rule($mlm->l_agent_id, $mlm)){
                $l_agent_commission = ConstVar::$VIP_COMMISSION_L_AGENT;
                if($mlm->s_agent_id){
                    $MLMBalanceLog->add(
                        $this->user_id,
                        $mlm->s_agent_id,
                        ConstVar::$VIP_COMMISSION_S_AGENT * $discount,
                        ConstVar::$MLM_BALANCE_LOG_TYPE_AGENT
                    );
                    $mlm_balance = MLMBalance::firstOrCreate(['user_id' => $mlm->s_agent_id]);
                    $mlm_balance->increment('balance', ConstVar::$VIP_COMMISSION_S_AGENT * $discount);
                    $mlm_balance->increment('total_balance', ConstVar::$VIP_COMMISSION_S_AGENT * $discount);
                    $l_agent_commission = $l_agent_commission - ConstVar::$VIP_COMMISSION_S_AGENT;
                }
                if($mlm->m_agent_id){
                    $MLMBalanceLog->add(
                        $this->user_id,
                        $mlm->m_agent_id,
                        ConstVar::$VIP_COMMISSION_M_AGENT * $discount,
                        ConstVar::$MLM_BALANCE_LOG_TYPE_AGENT
                    );
                    $mlm_balance = MLMBalance::firstOrCreate(['user_id' => $mlm->m_agent_id]);
                    $mlm_balance->increment('balance', ConstVar::$VIP_COMMISSION_M_AGENT * $discount);
                    $mlm_balance->increment('total_balance', ConstVar::$VIP_COMMISSION_M_AGENT * $discount);
                    $l_agent_commission = $l_agent_commission - ConstVar::$VIP_COMMISSION_M_AGENT;
                }
                $MLMBalanceLog->add(
                    $this->user_id,
                    $mlm->l_agent_id,
                    $l_agent_commission * $discount,
                    ConstVar::$MLM_BALANCE_LOG_TYPE_AGENT
                );
                $mlm_balance = MLMBalance::firstOrCreate(['user_id' => $mlm->l_agent_id]);
                $mlm_balance->increment('balance', $l_agent_commission * $discount);
                $mlm_balance->increment('total_balance', $l_agent_commission * $discount);
            }

            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            throw new \Exception($e->getMessage());
        }
    }

    private function rule($agent_id, $mlm){
        if($agent_id != $mlm->s_parent_id && $agent_id != $mlm->m_parent_id && $agent_id != $mlm->l_parent_id){
            return true;
        }
        return false;
    }
}
